package com.wanmait.wanmarecruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wanmait.wanmarecruit.entity.Education;
import com.wanmait.wanmarecruit.entity.Position;
import com.wanmait.wanmarecruit.mapper.PositionMapper;
import com.wanmait.wanmarecruit.service.SalaryService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanmait.wanmarecruit.mapper.SalaryMapper;
import com.wanmait.wanmarecruit.entity.Salary;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary> implements SalaryService {

    @Resource
    private SalaryMapper salaryMapper;

    @Resource
    private PositionMapper positionMapper;

    @Override
    public List<Salary> getAllSalary() {
        LambdaQueryWrapper<Salary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Salary::getSalaryInfo,Salary::getId);
        queryWrapper.orderByAsc(Salary::getSort);
        return this.list(queryWrapper);
    }

    @Override
    public List<Salary> getAllSalaryAndPositionCount() {
        LambdaQueryWrapper<Salary> lambdaQueryWrapper = new LambdaQueryWrapper();
        List<Salary> salaries = salaryMapper.selectList(lambdaQueryWrapper);

        salaries.forEach(salary -> {
            LambdaQueryWrapper<Position> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(Position::getSalaryId,salary.getId());
            Integer positionCount = positionMapper.selectCount(lambdaQueryWrapper1).intValue();
            salary.setPositionCount(positionCount);
        });

        return salaries;
    }
}
